package cn.example.web.db.dao;

import org.artifact.core.db.MySqlDao;
import org.artifact.core.lang.StrPlus;
import java.util.List;
import java.util.ArrayList;
import java.text.MessageFormat;
import java.util.function.BiFunction;
import org.artifact.core.db.BaseBean;
import cn.hutool.core.map.MapUtil;
import java.util.Map;
import java.util.HashMap;
import org.artifact.core.db.BaseDao;
import cn.example.web.db.bean.GameCodeGroup;
import static cn.hutool.core.util.ObjectUtil.equal;

public abstract class GameCodeGroupDAO extends MySqlDao<GameCodeGroup>{
	
	protected final String sqlAllColumn = "id, type, total_use_count, single_use_count, status, body, effective_date, invalid_date, exclusive_channel, exclusive_channel_app, exclusive_os, use_log, create_date, modified_date";

	protected final String sqlAllPla = "?,?,?,?,?,?,?,?,?,?,?,?,?,?";

	@Override
	protected String getDBName() {
		return "example_web";
	}
	
	@Override
	protected String getTableName() {
		return "t_game_code_group";
	}
	
	@Override
	protected Class<GameCodeGroup> getEntityClass(){
		return GameCodeGroup.class;
	}
	
	@Override
	protected GameCodeGroup createEntity() {
		return new GameCodeGroup() {};
	}

	@Override
	protected String getPKFieldName() {
		return "id";
	}
	
	// 根据主键导出
	public Map<String,Object> exportByPrimaryKey(long id,BiFunction<BaseDao,BaseBean,Object> function){
		return export(findByPrimaryKey(id),function);
	}
	
	
	public Map<String,Object> export(GameCodeGroup gameCodeGroup,BiFunction<BaseDao,BaseBean,Object> function){
		Map<String,Object> data = new HashMap<>();
		data.put("self", function.apply(this,gameCodeGroup));
		Map<String,Object> nodes = new HashMap<>();
		data.put("nodes", nodes);
		/**
		 * 此处可优化为并发加载，可选方式有executor.invokeAll、forkjoin都可以。暂时不做优化处理
		 */
		nodes.put("gameCodeEntry.groupId",getGameCodeEntryDAO().exportByGroupId((long) gameCodeGroup.pk(),function));
		return data;
	}
	
	public List<Map<String,Object>> export(List<GameCodeGroup> gameCodeGroups,BiFunction<BaseDao,BaseBean,Object> function){
		List<Map<String,Object>> data = new ArrayList<>();
		for(GameCodeGroup gameCodeGroup : gameCodeGroups){
			data.add(export(gameCodeGroup,function));
		}
		return data;
	}


	public GameCodeGroup _import(Map<String,Object> data,BiFunction<BaseDao,Map,BaseBean> function){
	    GameCodeGroup gameCodeGroup = createEntity();
	    gameCodeGroup = (GameCodeGroup)function.apply(this,MapUtil.get(data,"self",Map.class));

        Map<String,Object> nodes = MapUtil.get(data,"nodes",Map.class);
        /**
         * 此处可优化为并发加载，可选方式有executor.invokeAll、forkjoin都可以。暂时不做优化处理
         */
        getGameCodeEntryDAO()._import(MapUtil.get(nodes,"gameCodeEntry.groupId",List.class),function);
        return gameCodeGroup;
    }

    public List<GameCodeGroup> _import(List<Map<String,Object>> data,BiFunction<BaseDao,Map,BaseBean> function){
        List<GameCodeGroup> list = new ArrayList<>();
        for(Map<String,Object> map : data){
            list.add(_import(map,function));
        }
        return list;
    }
	
	protected abstract GameCodeEntryDAO getGameCodeEntryDAO();
	
	@Override
	public boolean createTable(){
		StrPlus plus = StrPlus.b("CREATE TABLE IF NOT EXISTS `t_game_code_group` (");
		plus.a("  `id` bigint(20) NOT NULL COMMENT '主键',");
		plus.a("  `type` varchar(45) NOT NULL COMMENT '类型',");
		plus.a("  `total_use_count` varchar(45) NOT NULL COMMENT '总次数',");
		plus.a("  `single_use_count` varchar(45) NOT NULL COMMENT '单人次数',");
		plus.a("  `status` varchar(45) NOT NULL COMMENT '状态 0.待激活 1.生效中 2.已失效',");
		plus.a("  `body` varchar(45) NOT NULL COMMENT '内容',");
		plus.a("  `effective_date` varchar(45) NOT NULL COMMENT '生效时间',");
		plus.a("  `invalid_date` varchar(45) NOT NULL COMMENT '失效时间',");
		plus.a("  `exclusive_channel` varchar(45) NOT NULL COMMENT '专属渠道',");
		plus.a("  `exclusive_channel_app` varchar(45) NOT NULL COMMENT '专属包',");
		plus.a("  `exclusive_os` varchar(45) NOT NULL COMMENT '专属平台',");
		plus.a("  `use_log` varchar(45) NOT NULL COMMENT '使用记录',");
		plus.a("  `create_date` datetime NOT NULL COMMENT '创建时间',");
		plus.a("  `modified_date` datetime NOT NULL COMMENT '修改时间',");
		plus.a("  PRIMARY KEY (`id`)");
		plus.a(") ENGINE=InnoDB DEFAULT CHARSET=utf8  COMMENT='游戏码组';");
		update(plus.e());
		return true;
	}
	
	@Override
	protected String getSaveSql() {
		return MessageFormat.format("REPLACE INTO {0} ({1}) VALUES ({2})",getTableName(),sqlAllColumn,sqlAllPla);
	}

	@Override
	protected String getDeleteSql() {
		return MessageFormat.format("DELETE FROM {0} WHERE id = ?",getTableName());
	}
	
}